package com.lb.service;

import com.lb.annotation.MongoSwitch;
import com.lb.entity.User;
import com.lb.utils.R;
import com.mongodb.BasicDBObject;
import io.undertow.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @version 1.0
 * @Author: Edward
 * @Date: 2021/7/3 15:00
 * @Description:
 */
@Slf4j
@Service
public class UserService {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存用户
     */
    @MongoSwitch("dbPeople")
    public R savePeople(User user) {
        mongoTemplate.insert(user);
        return R.ok();
    }

    @MongoSwitch("admin")
    public R savePeopleToAdmin(User user) {
        mongoTemplate.insert(user);
        return R.ok();
    }

    public R savePeopleToDefault(User user) {
        mongoTemplate.insert(user);
        return R.ok();
    }

    @MongoSwitch("sysLog")
    public R test(User user) {
        saveBizLog(user);

        int i = 9 / 0;
        log.info("==============系统日志===============");

        return R.ok();
    }

    @MongoSwitch("bizLog")
    private void saveBizLog(User user) {
        final BasicDBObject bsonLog = new BasicDBObject();
        bsonLog.append("id", 10000);
        bsonLog.append("appname", "zbl-hds-web");
        bsonLog.append("message", user);
        bsonLog.append("dataType", "object:java.util.HashMap");
        bsonLog.append("logTimeStamp", DateUtils.toDateString(new Date()));
        bsonLog.append("logTime", DateUtils.toDateString(new Date()));
        bsonLog.append("createTime", DateUtils.toDateString(new Date()));
        bsonLog.append("createTimeStamp", DateUtils.toDateString(new Date()));
        mongoTemplate.insert(bsonLog, "dev-zbl-hds-web-127.0.0.1-9991-biz-log");
    }

}
